package cn.pomelo.signal.quartZ;

import cn.pomelo.signal.common.CSVFileUtil;
import cn.pomelo.signal.common.SpringContextJobUtil;
import cn.pomelo.signal.config.SignalConfig;
import cn.pomelo.signal.model.Trade;
import cn.pomelo.signal.service.TradeService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DataUpdateJob implements Job {

    @Override
    public void execute(JobExecutionContext context) {
        Date now = new Date();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        long start = now.getTime();
        Logger logger = LoggerFactory.getLogger(getClass());
        logger.info("数据更新开始：" + date);
        TradeService tradeService = (TradeService) SpringContextJobUtil.getBean("tradeService");
        try {
            List<Trade> trades = CSVFileUtil.readLatestData(SignalConfig.getLatest());
            logger.info(String.valueOf(trades.size()));
            // 插入数据库
            tradeService.batchInsert(trades);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Date end = new Date();
        long ended = end.getTime();
        logger.info("数据更新结束，共用时：" + (ended - start) / 1000 + "s");
    }
}
